function Cache () {
  this.storage = null
  this.user = 'USER'
  this.tokenKey = 'TOKEN_KEY'
  this.equipmentidKey = 'EQUIPMENTID_KEY'
}
Cache.prototype = {
  setToken: function (token) {
    return this.__setItem(this.tokenKey, token)
  },
  getToken: function () {
    return this.__getItem(this.tokenKey)
  },
  setUser: function (user) {
    return this.__setItem(this.user, user)
  },
  getUser: function () {
    return this.__getItem(this.user)
  },
  setEquipmentId: function (equipmentId) {
    return this.__setItem(this.equipmentidKey, equipmentId)
  },
  getEquipmentId: function () {
    return this.__getItem(this.equipmentidKey)
  },
  clearToken: function () {
    return this.__removeItem(this.tokenKey)
  },
  __check: function () {
    if (this.storage == null) {
      this.storage = sessionStorage
      this.setEquipmentId('8D3E8E0CF883C4E99329AF8A29300AB6')
    }
    return this.storage
  },
  __setItem: function (key, value) {
    const storage = this.__check()
    return new Promise((resolve, reject) => {
      storage.setItem(key, value)
      resolve(true)
    })
  },
  __getItem: function (key) {
    const storage = this.__check()
    return new Promise((resolve, reject) => {
      resolve(storage.getItem(key))
    })
  },
  __removeItem: function (key) {
    const storage = this.__check()
    return new Promise((resolve, reject) => {
      resolve(storage.removeItem(key))
    })
  }
}
const che = new Cache()
export default che
